# coding=utf-8

"""
    Created by Kalyter on 2018-01-28.
"""

import requests

from learning.scientifc import writer
from learning.scientifc.jobs.job import Job


class CrawlJob(Job):
    name = '中国海关进出口爬虫作业'

    def run(self):
        # 1. 重现请求
        url = 'http://credit.customs.gov.cn/ccppAjax/queryDirectoryList.action'
        body = {
            'ccppListQueryRequest.manaType': 'AA;0',
            'ccppListQueryRequest.casePage.curPage': 1,
            'ccppListQueryRequest.casePage.pageSize': 4000
        }
        response = requests.post(url, body)
        data = response.json()
        print('接口请求成功')

        # 2. 将数据存储到Excel中
        data_list = data.get('responseResult', {})\
            .get('responseData', {}).get('copInfoResultList', [])
        result = []
        headers = ('序号', '统一社会信用代码', '企业组织机构代码', '企业名称')
        result.append(headers)

        for i, item in enumerate(data_list):
            element = (
                i + 1,
                item.get('socialCreditCode', ''),
                item.get('saicSysNo', ''),
                item.get('nameSaic', '')
            )
            result.append(element)

        writer.write(result)
